package com.example.dllo.keepdemo.dynamic;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.dynamic.bean.ExquisiteDynamicBean;
import com.example.dllo.keepdemo.tools.GsonRequest;
import com.example.dllo.keepdemo.tools.VolleySingleton;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 16/1/15.
 */
public class ExquisiteDynamicActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private ListView listView;
    private ExquisiteListViewAdapter adapter;
    private Toolbar toolbar;
    private ExquisiteDynamicBean exquisiteDynamicBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exquisitedynamic);
        listView = (ListView) findViewById(R.id.listview_activity_exquisitedynamic);
        toolbar = (Toolbar) findViewById(R.id.toolbar_activity_exquisitedynamic);
        listView.setSmoothScrollbarEnabled(true);
        listView.setOnItemClickListener(this);
        initData();

        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.mipmap.actionlastbtn);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ExquisiteDynamicActivity.this.finish();
            }
        });
    }

    private void initData() {
        exquisiteDynamicBean = new ExquisiteDynamicBean();
        String url = "https://api.gotokeep.com/v1.1/timeline/hot";
        GsonRequest<ExquisiteDynamicBean> gsonRequest = new GsonRequest<ExquisiteDynamicBean>(url, ExquisiteDynamicBean.class, new Response.Listener<ExquisiteDynamicBean>() {
            @Override
            public void onResponse(ExquisiteDynamicBean response) {
                exquisiteDynamicBean = response;
                adapter = new ExquisiteListViewAdapter(ExquisiteDynamicActivity.this,response);


                Intent intent = getIntent();
                int position = intent.getIntExtra("position",0);
                Log.d("ExquisiteDynamicActivit", "position:" + position);
                listView.smoothScrollToPosition(position);
                listView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String,String> map = new HashMap<>();
                map.put("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NjY1MDBlMTYzZGIwYzlmNTdiMTQwYWQiLCJ1c2VybmFtZSI6Iuaeq-WQmyIsImF2YXRhciI6Imh0dHA6Ly93eC5xbG9nby5jbi9tbW9wZW4vc2t3MmhSR3lXRWcwUzV0NzhkMlRVUnFhYjBzcWJ4RXB4S1BLUFpEOXpwbTNJbkIySzRjRnhLVEJsemdDb3VIcGlia3RkRUluQ0NFcGpIS2FHZ1lwRkp6SVRIVU5NYTNpYmYvMCIsImlhdCI6MTQ1MjU4OTkwNiwiZXhwIjoxNDU1MTgxOTA2LCJpc3MiOiJodHRwOi8vd3d3LmdvdG9rZWVwLmNvbS8ifQ.4I6FpbJT1_1fe8Z51oHt6Mv5t-3zXGGYhI1rMe3U8gM");
                map.put("X-X", "486bba2566a02d3ae31ed4445366a873b89cf910");
                map.put("Host", "api.gotokeep.com");
                map.put("X-KEEP-VERSION", "2.8.1");
                map.put("X-KEEP-CHANNEL", "keep");
                return map;
            }
        };

        VolleySingleton.getInstance()._addRequest(gsonRequest);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this,DynamicDetailActivity.class);
        intent.putExtra("id",exquisiteDynamicBean.getData().get(position).get_id());
        startActivity(intent);
    }
}
